<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Using OpenCV Java with Eclipse</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Using OpenCV Java with Eclipse </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d9/d52/tutorial_java_dev_intro.html">Introduction to Java Development</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d7/d1e/tutorial_clojure_dev_intro.html">Introduction to OpenCV Development with Clojure</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Barış Evrim Demiröz </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<dl class="section warning"><dt>Warning</dt><dd>This tutorial can contain obsolete information.</dd></dl>
<p>Since version 2.4.4 <a href="http://opencv.org/opencv-java-api.html">OpenCV supports Java</a>. In this tutorial I will explain how to setup development environment for using OpenCV Java with Eclipse in <b>Windows</b>, so you can enjoy the benefits of garbage collected, very refactorable (rename variable, extract method and whatnot) modern language that enables you to write code with less effort and make less mistakes. Here we go.</p>
<h2>Configuring Eclipse </h2>
<p>First, obtain a fresh release of OpenCV <a href="http://opencv.org/releases.html">from download page</a> and extract it under a simple location like <code>C:\OpenCV-2.4.6\</code>. I am using version 2.4.6, but the steps are more or less the same for other versions.</p>
<p>Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any project. Launch Eclipse and select Window &ndash;&gt; Preferences from the menu.</p>
<div class="image">
<img src="../../1-window-preferences.png" alt="1-window-preferences.png"/>
</div>
<p>Navigate under Java &ndash;&gt; Build Path &ndash;&gt; User Libraries and click New....</p>
<div class="image">
<img src="../../2-user-library-new.png" alt="2-user-library-new.png"/>
</div>
<p>Enter a name, e.g. OpenCV-2.4.6, for your new library.</p>
<div class="image">
<img src="../../3-library-name.png" alt="3-library-name.png"/>
</div>
<p>Now select your new user library and click Add External JARs....</p>
<div class="image">
<img src="../../4-add-external-jars.png" alt="4-add-external-jars.png"/>
</div>
<p>Browse through <code>C:\OpenCV-2.4.6\build\java\</code> and select opencv-246.jar. After adding the jar, extend the opencv-246.jar and select Native library location and press Edit....</p>
<div class="image">
<img src="../../5-native-library.png" alt="5-native-library.png"/>
</div>
<p>Select External Folder... and browse to select the folder <code>C:\OpenCV-2.4.6\build\java\x64</code>. If you have a 32-bit system you need to select the x86 folder instead of x64.</p>
<div class="image">
<img src="../../6-external-folder.png" alt="6-external-folder.png"/>
</div>
<p>Your user library configuration should look like this:</p>
<div class="image">
<img src="../../7-user-library-final.png" alt="7-user-library-final.png"/>
</div>
<h2>Testing the configuration on a new Java project </h2>
<p>Now start creating a new Java project.</p>
<div class="image">
<img src="../../7_5-new-java-project.png" alt="7_5-new-java-project.png"/>
</div>
<p>On the Java Settings step, under Libraries tab, select Add Library... and select OpenCV-2.4.6, then click Finish.</p>
<div class="image">
<img src="../../8-add-library.png" alt="8-add-library.png"/>
</div>
<div class="image">
<img src="../../9-select-user-lib.png" alt="9-select-user-lib.png"/>
</div>
<p>Libraries should look like this:</p>
<div class="image">
<img src="../../10-new-project-created.png" alt="10-new-project-created.png"/>
</div>
<p>Now you have created and configured a new Java project it is time to test it. Create a new java file. Here is a starter code for your convenience: </p><div class="fragment"><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>Hello</div><div class="line">{</div><div class="line">   <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main( <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args )</div><div class="line">   {</div><div class="line">      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );</div><div class="line">      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );</div><div class="line">      System.out.println( <span class="stringliteral">&quot;mat = &quot;</span> + mat.dump() );</div><div class="line">   }</div><div class="line">}</div></div><!-- fragment --><p> When you run the code you should see 3x3 identity matrix as output.</p>
<div class="image">
<img src="../../11-the-code.png" alt="11-the-code.png"/>
</div>
<p>That is it, whenever you start a new project just add the OpenCV user library that you have defined to your project and you are good to go. Enjoy your powerful, less painful development environment :)</p>
<h2>Running Java code with OpenCV and MKL dependency </h2>
<p>You may get the following error (e.g. on Ubuntu) if you have built OpenCV with MKL library with some Java code that calls OpenCV functions that use Intel MKL: </p><blockquote class="doxtable">
<p>Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so. </p>
</blockquote>
<p>One solution to solve this on Linux consists in preloading the Intel MKL library (either run the command in a terminal or add it to your <code>.bashrc</code> file). Your command line should be something similar to this (add <code>$LD_PRELOAD:</code> before if you have already set the <code>LD_PRELOAD</code> variable): </p><blockquote class="doxtable">
<p>export LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_sequential.so </p>
</blockquote>
<p>Then, run the Eclipse IDE from a terminal that have this environment variable set (<code>echo $LD_PRELOAD</code>) and the error should disappear. </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
